package com.accgm.api.db.dao;

import java.lang.reflect.Field;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.accgm.api.db.dao.base.AccgmUserMessageDaoBase;
import com.accgm.api.utils.LogUtils;
import com.accgm.api.utils.ReflectUtils;
import com.coola.jutil.sql.DBEngine;
import com.coola.jutil.sql.DBFactory;

/**
 * 这是accgm_user_message的Dao实现类
 */
public class AccgmUserMessageDao extends AccgmUserMessageDaoBase   {
    /**
     * AccgmUserMessageDao对象
     */
    private static AccgmUserMessageDao instance = new AccgmUserMessageDao();

    /**
     * 默认构造方法
     */
    private AccgmUserMessageDao() {
    }

    /**
     * 获得对象实例
     */
    public static AccgmUserMessageDao getInstance() { 
         return instance;
    } 
	/**
	 * 插入一条数据
	 * 
	 * @param model
	 * @return
	 */
	public int insert(Object model) {
		DBEngine localEngine = DBFactory.getKeepConnDBEngine("main-statistic");
		StringBuffer sql = new StringBuffer("INSERT INTO " + TABLE + " (");
		StringBuffer stmts = new StringBuffer(" values(");
		List<Object> sqlArgs = new ArrayList<Object>();
		Field[] superfields = model.getClass().getSuperclass().getDeclaredFields();
		for (Field field : superfields) {
			if (field.getName().equals("serialVersionUID") || field.getName().equals("id")) {
				continue;
			}
			if (ReflectUtils.getter(model, field.getName()) != null) {
				sql.append(" " + field.getName() + ",");
				stmts.append("?,");
				sqlArgs.add(ReflectUtils.getter(model, field.getName()));
			}
		}

		String sqlstr = sql.toString();
		sqlstr = sqlstr.substring(0, sqlstr.length() - 1);
		sqlstr = sqlstr + ")";

		String stmtstr = stmts.toString();
		stmtstr = stmtstr.substring(0, stmtstr.length() - 1);
		stmtstr = stmtstr + ")";

		LogUtils.log.info(sqlstr + stmtstr);

		Object[] paramObj = sqlArgs.toArray();
		int id = 0;
		try {
			String tSql = sqlstr + stmtstr;
			id = localEngine.executeUpdate(tSql, paramObj);
			ResultSet rs = localEngine.executeQuery("select last_insert_id() as id");
			if (rs.next()) {
				id = rs.getInt("id");
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			localEngine.close();
		}
		return id;
	}

	/**
	 * 更新操作
	 * 
	 * @param model
	 * @return
	 */
	public boolean updateAny(Object model) {
		StringBuffer sql = new StringBuffer("update  " + TABLE + " set ");
		List<Object> sqlArgs = new ArrayList<Object>();
		Field[] superfields = model.getClass().getSuperclass().getDeclaredFields();
		for (Field field : superfields) {
			if (field.getName().equals("serialVersionUID") || field.getName().equals("id")) {
				continue;
			}
			if (ReflectUtils.getter(model, field.getName()) != null) {
				sql.append(" " + field.getName() + "=? ,");
				sqlArgs.add(ReflectUtils.getter(model, field.getName()));
			}
		}

		String sqlstr = sql.toString();
		sqlstr = sqlstr.substring(0, sqlstr.length() - 1);
		sqlstr = sqlstr + " where id=?";
		sqlArgs.add(ReflectUtils.getter(model, "id"));
		LogUtils.log.info(sqlstr);
		LogUtils.log.info(sqlArgs.toString());

		Object[] paramObj = sqlArgs.toArray();
		try {
			return writeDBEngine.executeUpdate(sqlstr, paramObj) > 0;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}    
    
}
